/*
 * InputManager.cpp
 *
 *  Created on: 03/08/2010
 *      Author: alex
 */

#include <cstring>
#include "InputManager.h"

using std::vector;
using utils::playerNumber;
using utils::MAX_PLAYERS;


InputManager inputManager;


InputManager::InputManager() :
keys(MAX_PLAYERS, vector<char>(350, 0)), quit(false) {
	
	// acho que vai ser sempre CLIENTE porém não sei
//	networkManager = new networkManager(CLIENT);
}


InputManager& InputManager::pollEvents() {
	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_KEYDOWN:
			keys[playerNumber][event.key.keysym.sym] = 1;
			if (event.key.keysym.sym == SDLK_j)
				keys[1][SDLK_LEFT] = 1;
			if (event.key.keysym.sym == SDLK_l)
				keys[1][SDLK_RIGHT] = 1;
			if (event.key.keysym.sym == SDLK_i)
				keys[1][SDLK_UP] = 1;
			if (event.key.keysym.sym == SDLK_y)
				keys[1][SDLK_a] = 1;
			// to mandando referência da tecla apertada
			//uma forma melhor, seria enviar ao servidor e o servidor gerencia
			// por enquanto fica assim
			
			//networkManager->broadcast("SDL_KEYDOWN",11);
			break;
		case SDL_KEYUP:
			keys[playerNumber][event.key.keysym.sym] = 0;
			if (event.key.keysym.sym == SDLK_j)
				keys[1][SDLK_LEFT] = 0;
			if (event.key.keysym.sym == SDLK_l)
				keys[1][SDLK_RIGHT] = 0;
			if (event.key.keysym.sym == SDLK_i)
				keys[1][SDLK_UP] = 0;
			if (event.key.keysym.sym == SDLK_y)
				keys[1][SDLK_a] = 0;
			//networkManager->broadcast("SDL_KEYUP",9);
			break;
		case SDL_QUIT:
			quit = true;
			break;
		}
	}
	return *this;
}
